﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Text;
using SilverlightAndAspdotNetMVC.Web.Models;

namespace SilverlightAndAspdotNetMVC.Web.Daos
{
    public class FileWriter
    {
        public MonitorDataEntity Read(string filePath)
        {
            //DataCollector.Entity.MonitorDataEntity
            if (!File.Exists(filePath))
            {
                return null;
            }
            while (true)
            {
                try
                {
                    string allText = File.ReadAllText(filePath);
                    if (allText != null)
                    {
                        string[] resTextAry = allText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                        if (resTextAry.Length == 1)
                        {
                            string[] ary = resTextAry[0].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                            MonitorDataEntity sr = new MonitorDataEntity();
                            // sr.Id = int.Parse(ary[0]);
                            sr.ExportTime = ary[0];
                            sr.Temperature = double.Parse(ary[1]);
                            sr.Humidity = double.Parse(ary[2]);
                            sr.Illumination = double.Parse(ary[3]);
                            return sr;
                        }
                    }
                    throw new Exception();
                }
                catch
                {
                    continue;
                }
            }

        }
    }
}